<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/17
 * Time: 11:27
 */
namespace app\activity\controller;

use app\common\model\Autoreply;
use app\common\model\AutoreplyContent;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Admin extends AdminBase
{
    private $autoreply_type = 15;

    public function index()
    {
        $status = Request::param('status', 1);

        $filter = array('company_id'=>$this->company_id, 'open_card_id'=>$this->open_card_id, 'type'=>$this->autoreply_type);
        $list   = getList('autoreply',$filter, ['id', 'desc'], 0);

        foreach ($list as $k => $v) {
            $info = _uri('autoreply_content', array('autoreply_id'=>$v['id']));
            $list[$k]['content'] = $info['content'];
        }

        return View::fetch('index',[
            'status' => $status,
            'list'   => $list,
        ]);
    }

    /**
     * 添加
     */
    public function add()
    {
        return View::fetch('add');
    }

    public function save()
    {
        $id      = Request::param('id', 0);
        $title   = filter_string(Request::param('title'));
        // 回复的内容
        $content  = Request::param('content');

        $content  = customer_content_filter($content);


        if (!$title) {
            return ['info' => '标题不能为空'];
        }

        if (!$content) {
            return ['info' => '回复内容不能为空'];
        }

        $filter = array('company_id'=>$this->company_id, 'open_card_id'=>$this->open_card_id, 'type'=>$this->autoreply_type,'title'=>$title);
//        $autoreply_id = _uri('autoreply', $filter, 'id');
//        if (!$autoreply_id['id']) {

//        }
        if ($id) {
            Autoreply::update($filter, ['id' => $id]);
            $autoreply_id = $id;
        } else {
            $autoreply  = Autoreply::create($filter);
            $autoreply_id = $autoreply->id;
        }

        // 辅表
        $info = _uri('autoreply_content', array('autoreply_id'=>$autoreply_id));
        if (!$info) {
            AutoreplyContent::create([
                'autoreply_id' => $autoreply_id,
                'content_type' => $this->autoreply_type,
                'content'      => html_entity_decode($content)
            ]);
        } else {
            AutoreplyContent::update(['content_type' => $this->autoreply_type, 'content' => html_entity_decode($content)], ['autoreply_id' => $autoreply_id]);
        }

        if (Request::isAjax()) {
            return ['info' => 'ok'];
        }

        return redirect(url('/activity/admin/index'));
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id         = Request::param('id', 0);
        if (!$id) {
            return ['info' =>'请选择要编辑的信息'];
        }

        $info = _uri('autoreply', ['id' => $id]);
        if (!$info) {
            return ['info' => '该信息不存在或已删除'];
        }
        $content = _uri('autoreply_content', array('autoreply_id'=>$info['id']));
        if ($content) {
            $info['content'] = $content['content'];
        }

        return View::fetch('add', [
            'info' => $info
        ]);
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = Request::param('id');
        if (!$id) {
            return ['info' => '对不起，请选择您要删除的信息！'];
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $v) {
            $info = _uri('autoreply', ['id' => $v]);
            if (!$info) {
                continue;
            }

            if ($info['status'] == 0) {
                Autoreply::destroy($v);
            } else {
                Autoreply::update(['status' => 0], ['id' => $v]);
            }
        }

        return ['info' => "ok"];
    }

    /**
     * 更改状态，如还原等
     */
    public function change_status()
    {
        $status = Request::param('status', 0);
        $id = Request::param('id', 0);

        if (!$id) {
            return ["info" => '对不起，请选择您要操作的信息！'];
        }

        $info = _uri('autoreply', ['id' => $id]);
        if ($info['status'] == $status) {
            return ["info" => '已经执行过操作，不用重复提交'];
        }

        Autoreply::update(['status' => $status], ['id' => $id]);

        return array('info' => 'ok');
    }
}